package com.ciwong.epaper.modules.bookstore.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.NewBook;
import com.ciwong.epaper.modules.epaper.ui.AllServiceOrBookActivity;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.DisplayUtils;
import com.ciwong.epaper.util.RequestCode;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 */

public class ItemGoodBookList extends LinearLayout {

    private RecyclerView item_recyclerview;
    private GoodBookHorizontalAdapter mHorizontalAdapter;
    private  TextView tx_no_book_tips;

    private LinearLayout btn_check_all;

    public ItemGoodBookList(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.item_good_book_list, this);
        item_recyclerview = findViewById(R.id.item_recyclerview);
        tx_no_book_tips=findViewById(R.id.tx_no_book_tips);

        init();
    }

    protected void init() {
        item_recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mHorizontalAdapter = new GoodBookHorizontalAdapter();
        item_recyclerview.setAdapter(mHorizontalAdapter);

        findViewById(R.id.btn_check_all).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                EpaperJumpManager.jumpToAllServiceOrBook(R.string.go_back, (Activity) getContext(), AllServiceOrBookActivity.TYPE_HOT_GOOD_BOOKS, 0,null);
            }
        });
    }


    public void update(List<NewBook> newBookList) {

        if (null != newBookList && newBookList.size() > 0) {
            mHorizontalAdapter.setNewBookArrayList(newBookList);
            tx_no_book_tips.setVisibility(GONE);
            item_recyclerview.setVisibility(VISIBLE);
        }else {
            newBookList =new ArrayList<>();
            mHorizontalAdapter.setNewBookArrayList(newBookList);
            tx_no_book_tips.setVisibility(VISIBLE);
            item_recyclerview.setVisibility(GONE);
        }

    }


    /**
     * 内层的RecyclerView
     */
    private class GoodBookHorizontalAdapter extends RecyclerView.Adapter<GoodBookHolder> {

        private List<NewBook> newBookArrayList = new ArrayList<>();

        private GoodBookHorizontalAdapter() {

        }

        public void setNewBookArrayList(List<NewBook> list) {
            newBookArrayList = list;
            notifyDataSetChanged();
        }

        @Override
        public GoodBookHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = null;
            itemView = inflate(parent, R.layout.item_good_book);
            return new GoodBookHolder(itemView);
        }

        /**
         * @param holder
         * @param position
         */
        @Override
        public void onBindViewHolder(GoodBookHolder holder, final int position) {
            Uri uri;
            if (null != newBookArrayList && position < newBookArrayList.size()) {

                NewBook newBook = newBookArrayList.get(position);
                holder.tx_book_name.setText(newBook.getServiceName());
                uri = Uri.parse(newBook.getLogo());
//                setNetImg(holder.sv_good_book, uri);
//                RoundingParams bitmapOnlyParams = RoundingParams.fromCornersRadius(10) .setRoundingMethod(RoundingParams.RoundingMethod.BITMAP_ONLY);
//                holder.sv_good_book.getHierarchy().setRoundingParams(bitmapOnlyParams);
//                holder.sv_good_book.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
//                holder.sv_good_book.setImageURI(uri);
                setNetImg(holder.sv_good_book,uri);
                holder.mNewBook=newBook;

//                holder.tv_subject.setText(subject.getSubjectName());
//                holder.img_subject.setImageResource( R.mipmap.chinese);
//                holder.mView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        // 外面不区分，里面区分
//                        MeJumpManager.jumpBuyService((Activity) getContext(), R.string.go_back, mNewBook.getServiceId(), -1, MainActivity.REQUEST_CODE_TO_PAY, null);
//                        MallJumpManager.jumpToGoodsList(0, (Activity) getContext(), appointmentTypeList.get(position), GoodsListActivity2.SEARCH_TYPE_GOODS_LIST_BY_CODE, GdConstants.ENTRANCE_TYPE_APPOINTMENT);
//                    }
//                });

            }

        }

        @Override
        public int getItemCount() {
            if (null != newBookArrayList) {
                return newBookArrayList.size();
            } else {
                return 0;
            }
        }
    }

    class GoodBookHolder extends RecyclerView.ViewHolder {
        TextView tx_book_name;
        SimpleDraweeView sv_good_book;
        public final View mView;
        NewBook mNewBook;


        public GoodBookHolder(View itemView) {
            super(itemView);
            mView = itemView;
            tx_book_name = (TextView) itemView.findViewById(R.id.tx_book_name);
            sv_good_book = (SimpleDraweeView) itemView.findViewById(R.id.sv_good_book);
            mView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 外面不区分，里面区分
                    MeJumpManager.jumpBuyService((Activity) getContext(), R.string.go_back, mNewBook.getServiceId(), -1, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY, null);
                }
            });

        }
    }


    protected View inflate(ViewGroup parent, int layoutRes) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
    }

    private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri) {
        try {
            ImageRequest request = null;
            if (uri != null) {
                request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(getContext(), 300), DisplayUtils.dip2px(getContext(), 420)))
                        .build();
                RoundingParams bitmapOnlyParams = RoundingParams.fromCornersRadius(10)
                        .setRoundingMethod(RoundingParams.RoundingMethod.OVERLAY_COLOR)
                        .setOverlayColor(Color.WHITE);
                GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
                //			builder.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
                builder.setRoundingParams(bitmapOnlyParams);
                GenericDraweeHierarchy hierarchy = builder.build();
                hierarchy.setPlaceholderImage(R.mipmap.item_load_icon);
                simpleDraweeView.setHierarchy(hierarchy);
                DraweeController drawweeController = Fresco.newDraweeControllerBuilder()
                        .setUri(uri)
                        .setImageRequest(request)
                        //列表滑动会变流畅
                        .setOldController(simpleDraweeView.getController())
                        .build();
                simpleDraweeView.setController(drawweeController);
            }
        }catch (Exception e){

        }catch (OutOfMemoryError error){

        }

    }

}
